<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class transfer_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
		


	public function get_branch(){
			$q = $this->db->get('branch');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
	}
	
public function get_product(){
	
		$this->db->select('product.*,category.name AS category_name,company.name AS company_name' );
		$this->db->from('product', 'category','company' );
		$this->db->join('category', 'product.id_category = category.id' );
		$this->db->join('company', 'product.id_company = company.id');
		//$this->db->where(array('product.code' => $code));
		$q = 	$query = $this->db->get();
		$product = $q->result();
		$new_product=array();
		foreach($product as $data){
			//$data['product'] = $data['code'].'/'.$data['category_name'].'/'.$data['company_name'];	
			$new_product[]=$data;
			
		}
		///print_r($new_product);exit;
		return  $new_product; 
	}
	
	/*
	public function get_product(){
		$q = $this->db->get('company');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
			return $data;
		}
		
	}*/
	
	public function check_branch_product($id_branch,$id_product, $quantity ){
	if($id_branch == 0){
	//	$product = $this->db->get_where('product',array('id' => $id_product))->row_array();
		$warehouse_product = $this->db->get_where('warehouse',array('id_product' => $id_product))->row_array();
		if(count($warehouse_product)!=0){
			if($warehouse_product['opening_stock'] >= $quantity){
		 		echo 1;
			 }	else{
		 		echo 2;
		 	}	
		}else{
			echo 3;
		}
		}else{
		//$product = $this->db->get_where('product',array('id' => $id_product))->row_array();
		$branch_product = $this->db->get_where('branch_product',array('id_product' => $id_product,'id_branch' => $id_branch))->row_array();
		if(count($branch_product)!=0){
			if($branch_product['opening_stock'] >= $quantity){
			 	echo 1;
			 }	else{
		 		echo 2;
		 	}
		}else{
			echo 3;
		}
	}	
	}
	
	
	public function get_destinaion_branch($id_branch){
		if($id_branch == 0){
				$q = $this->db->query('SELECT * FROM branch WHERE id!= '.$id_branch );
		if($q->num_rows() > 0) {
			$data =$q->result_array();
			$o='';
				$first = '
					<option value=""> -- Select Branch-- </option>';
					
			//print_r($data);exit;
foreach ( $data as  $subcategory) 
{
  $o.=  '<option value="'.$subcategory['id'].'" >'.$subcategory['name'].'</option>';
}
echo $first.$o;
		}	
		
		}else{
					$q = $this->db->query('SELECT * FROM branch WHERE id!= '.$id_branch );
						if($q->num_rows() > 0) {
						$data =$q->result_array();
						$o='';	
			//print_r($data);exit;
					$first = '
					<option value="0"> -- Warehouse -- </option>';
					foreach ( $data as  $subcategory) 
					{
  						$o.=  '<option value="'.$subcategory['id'].'" >'.$subcategory['name'].'</option>';
					}
						echo $first.$o;
					}	
			
		}
	
}	
public function insert_transfer($data)
{
$this->db->insert('transfer',$data);
return  $this->db->insert_id();

}

/*public function insert_branch($data){
		//$product = $this->db->get_where('product',array('id' => $data['id_product']))->row_array();
		$source_branch = $this->db->get_where('branch_product',array('id_branch' => $data['source_branch'],'id_product' => $data['id_product']))->row_array();
		if( $data['destination_branch']!= 0){
			$destination_branch = $this->db->get_where('branch_product',array('id_branch' => $data['destination_branch'],'id_product' => $data['id_product']))->row_array();
			if(count($destination_branch) > 0){
					$des_data['opening_stock'] =  $destination_branch['opening_stock'] + $data['quantity'];
					$source_data['opening_stock'] = $source_branch['opening_stock'] - $data['quantity'] ;
					$this->db->update('branch_product',$des_data,array('id_branch'=>$data['destination_branch'],'id_product' => $data['id_product']));
					$this->db->update('branch_product',$source_data,array('id_branch'=>$data['source_branch'],'id_product' => $data['id_product']));
	
			}else{
					$data1['id_product'] = $data['id_product'];
					$data1['opening_stock'] =  $data['quantity'];
					$data1['id_branch'] = $data['destination_branch'];
					$new_data['opening_stock'] = $source_branch['opening_stock'] - $data['quantity'] ;
					$this->db->insert('branch_product',$data1);
					$this->db->update('branch_product',$new_data,array('id'=>$data['source_branch'],'id_product' => $data['id_product']));
					
			}
		}else{
			
			$warehouse = $this->db->get_where('warehouse',array('id_product' => $data['id_product']))->row_array();
			if(count($warehouse) > 0){
					$warehouse_data['opening_stock'] =  $warehouse['opening_stock'] + $data['quantity'];
					$source_data['opening_stock'] = $source_branch['opening_stock'] - $data['quantity'] ;
					$this->db->update('warehouse',$warehouse_data,array('id_product' => $data['id_product']));
					$this->db->update('branch_product',$source_data,array('id_branch'=>$data['source_branch'],'id_product' => $data['id_product']));
	
			}else{
					
					$data1['id_product'] = $data['id_product'];
					$data1['opening_stock'] =  $data['quantity'];
					$new_data['opening_stock'] = $source_branch['opening_stock'] - $data['quantity'] ;
					$this->db->insert('warehouse',$data1);
					$this->db->update('branch_product',$new_data,array('id'=>$data['source_branch'],'id_product' => $data['id_product']));
					
			}
			
			
			
		}
		
	$this->db->insert('transfer_product',$data);	
}
	

	public function insert_warehouse($data){
		//$product = $this->db->get_where('product',array('id' => $data['id_product']))->row_array();
		$warehouse = $this->db->get_where('warehouse',array('id_product' => $data['id_product']))->row_array();
		$destination_branch = $this->db->get_where('branch_product',array('id_branch' => $data['destination_branch'],'id_product' => $data['id_product']))->row_array();
			if(count($destination_branch) > 0){
					$des_data['opening_stock'] =  $destination_branch['opening_stock'] + $data['quantity'];
					$warehouse1['opening_stock'] = $warehouse['opening_stock'] - $data['quantity'] ;
					$this->db->update('branch_product',$des_data,array('id_branch'=>$data['destination_branch'],'id_product' => $data['id_product']));
					$this->db->update('warehouse',$warehouse1,array('id_product' => $data['id_product']));
	
			}else{
					
					$data1['id_product']= $data['id_product'];
					$data1['opening_stock'] =  $data['quantity'];
					$data1['id_branch'] = $data['destination_branch'];
					$new_data['opening_stock'] = $warehouse['opening_stock'] - $data['quantity'] ;
					$this->db->insert('branch_product',$data1);
					$this->db->update('warehouse',$new_data,array('id_product' => $data['id_product']));
			}
			
		
		
	$this->db->insert('transfer_product',$data);	
		
	

	}*/
	
	
	public function get_transfer(){
	$q = $this->db->get('transfer_product');
	return $q;		

}	
public function  get_product_details($id_product){
	$this->db->select('product.*,category.name AS category_name,company.name AS company_name' );
	$this->db->from('product', 'category','company' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->where(array('product.id' => $id_product));
	$q = 	$query = $this->db->get();
	$product = $q->row_array();
	$product_name = $product['code'].",".$product['category_name'].",".$product['company_name'];
	//echo $product_name;exit; 
	return  $product_name; 
}

public function get_branch_name($id){
	if($id!=0){
		$q = $this->db->get_where('branch',array("id"=> $id));
		$branch = $q->row_array();
		return $branch['name'];
	}else{
		
		return 'Warehouse';
	}
}
public function get_branch_n($id)
{
return$this->db->get_where('branch',array("id"=> $id));
}

/*public function get(){
	   $this->datatables
			->select("id,product_name,quantity, source_name,destination_name,date")
			->from("transfer_product")		
			->add_column("Actions", 
			"<center>			<a href='edit_bank/$1'>
								<i class='icon-edit'></i>
								</a>
							    <a href='delete_bank/$1' onClick=\"return confirm('". $this->lang->line('alert_x_customer') ."')\">
								<i class='icon-trash'></i>
								</a></center>", "id")
			->unset_column('id');
		
		
	   return  $this->datatables->generate();
	
}*/
public function get()
	{
	
$this->db->select("transfer.id as id,transfer.transfer_no,transfer.source_branch_name,transfer.destination_branch_name,transfer.date,transfer.status");
$this->db->from('transfer','product');	
$this->db->where('transfer.status','Pending');
$q = 	$query = $this->db->get()->result();
return $q;
  }
public function get_approved()
{
$this->db->select("transfer.id as id,transfer.transfer_no,transfer.source_branch_name,transfer.destination_branch_name,transfer.date,transfer.status");
$this->db->from('transfer','product');	
$this->db->where('transfer.status','Approved');
$q = 	$query = $this->db->get()->result();
return $q;
}
public function get_product_id($code)
{
return $this->db->get_where('product',array('code' => $code,));


}
public function get_max_transfer()
{
$this->db->select_max('id');
return $this->db->get('transfer');

}
public function get_invoice()
{
return $this->db->get('company_profile');
}	
public function get_user($id)
{
return $this->db->get_where('users',array('id' => $id,));

}
public function get_branch_code($id)
{
return $this->db->get_where('branch',array('id' => $id,));

}
public function get_product_detail($id)
{ 
$data= $this->db->get_where('product',array('code' => $id));

return $data;

}
public function get_code_detail($id)
{ 


$this->db->select('*');
$this->db->from('product');
$this->db->where('code',trim($id));
$data = $this->db->get()->row();
return $data;

}
public function insert_transfer_product($data)
{
$this->db->insert('transfer_product',$data);	
}
public function update_source($source,$id_product,$quantity)
{
$data=$this->db->get_where('branch_product',array('id_branch' => $source,'id_product'=>$id_product,))->row();
$opening_stock=$data->opening_stock;
$new_opening_stock=intval($opening_stock)-intval($quantity);
$new_data=array('opening_stock'=>$new_opening_stock,);
$this->db->update('branch_product',$new_data,array('id_product' => $id_product,'id_branch'=>$source,));

}
public function update_destination($destination,$id_product,$quantity)
{
if($this->db->get_where('branch_product',array('id_branch' => $destination,'id_product'=>$id_product,))->num_rows()==0)
{
$new_opening_stock=$quantity;
$new_data=array('opening_stock'=>$new_opening_stock,);
$this->db->update('branch_product',$new_data,array('id_product' => $id_product,'id_branch'=>$source,));
} else
{
$data=$this->db->get_where('branch_product',array('id_branch' => $destination,'id_product'=>$id_product,))->row();
$opening_stock=$data->opening_stock;
$new_opening_stock=intval($opening_stock)+intval($quantity);
$new_data=array('opening_stock'=>$new_opening_stock,);
$this->db->update('branch_product',$new_data,array('id_product' => $id_product,'id_branch'=>$destination,));

}

}
public function get_transfer_detail($id)
{
$this->db->select('transfer.id,transfer.transfer_no,transfer.date,transfer.destination_branch,transfer.source_branch_name,transfer.destination_branch_name' );
$this->db->from('transfer' );
$this->db->where('transfer.id',$id);
$q = 	$query = $this->db->get();
return $q;
}
public function get_transfer_product($id)
{
$this->db->select('product.id,product.name,product.code,transfer_product.quantity,transfer_product.received_quantity' );
$this->db->from('transfer_product', 'product','transfer' );
$this->db->join('product', 'transfer_product.id_product=product.id' );
$this->db->join('transfer', 'transfer_product.transfer_id=transfer.id' );
$this->db->where('transfer.id',$id);
$q = 	$query = $this->db->get()->result();
return $q;

}
public function get_transfer_det($id)
{ 
$this->db->select('transfer.id,transfer.transfer_no,transfer.date,transfer.source_branch,transfer.destination_branch' );
$this->db->from('transfer', 'transfer_product' );
$this->db->join('transfer_product', 'transfer_product.transfer_id=transfer.id' );
$this->db->where('transfer_product.id',$id);
$q = 	$query = $this->db->get();
return $q;
}
public function get_transfer_pdt($id)
{ 
$this->db->select('product.name,product.code,transfer_product.id_product,transfer_product.quantity' );
$this->db->from('product', 'transfer_product','transfer' );
$this->db->join('transfer_product', 'transfer_product.id_product=product.id' );
$this->db->join('transfer','transfer_product.transfer_id=transfer.id');
$this->db->where('transfer.id',$id);
$q = 	$query = $this->db->get();
return $q->result();
}
public function update_transfer_product($previous_product,$data,$user,$data1,$id,$product_id)
{ 
$transfer=$this->db->get_where('transfer_product',array('transfer_id' => $id,'id_product'=>$product_id))->num_rows();

if($transfer==0)
{ 
$this->db->insert('transfer_product',$data);

}
else{ 
$this->db->update('transfer_product',$data,array('id_product' => $product_id,'transfer_id'=>$id,));
}

}
public function update_transfer_source($source,$product_id,$quantity,$destination,$user,$previous_product)
{
print_r($previous_product);
echo $product_id; exit();
if(($source==$user->source_branch) AND ($destination ==$user->destination_branch)  )
{ 
 
$data=$this->db->get_where('branch_product',array('id_branch' => $source,'id_product'=>$id_product,))->row();
$opening_stock=$data->opening_stock;
$new_stock=$opening_stock+intval($previous_product->quantity)-intval($quantity);
$stock=array('opening_stock'=>$new_Stock,);
$this->db->update('branch_product',$stock,array('id_branch' => $source,'id_product'=>$product_id,));
$data1=$this->db->get_where('branch_product',array('id_branch' => $destination,'id_product'=>$id_product,))->row();
$opening_stocks=$data1->opening_stock;
$new_stocks=$opening_stocks-intval($previous_product->quantity)+intval($quantity);
$stocks=array('opening_stock'=>$new_stocks,);
$this->db->update('branch_product',$stocks,array('id_branch' => $source,'id_product'=>$product_id,));
}
else{ exit();}
}
public function update_transfer($data,$id)
{
$this->db->update('transfer',$data,array('id' => $id,));

}
public function get_all_company()
{
return $this->db->get('company')->result();


}
function getby_code($price1,$price2,$location,$code,$name,$category,$company){
			$where = "product.code  LIKE '$code%' ";
			if($name!=''){
			$where.= "AND product.name  LIKE '$name%' ";
			}
			if($location!=''){
			$where.= "AND branch_product.id_branch  LIKE '$location%' ";
			}
			if($price1!=''){
			 if($price2!="")
			 {
			$where.= "AND product.selling_price  BETWEEN $price1 AND $price2 ";
			} else{
			$where.= " AND product.selling_price  >= $price1 ";
			}
			
			}
			else{}
			if($category!=''){
			$where.= "AND category.name  LIKE '$category%' ";
			}
			if($company!=''){
			$where.= "AND company.name  LIKE '$company%' ";
			}
	if($location !="")
	{
	$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit','branch_product' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
	$this->db->join('branch_product', 'branch_product.id_product=product.id');
		
	$this->db->where($where);
			$all_sales=$this->db->get();
				return $all_sales;
	}
	else{
	$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
	$this->db->where($where);
			$all_sales=$this->db->get();
				return $all_sales;
	}
			



}
function getby_price($price1,$price2,$location,$code,$name,$category,$company){
             if(!empty ($price2))
			 {
			$where = "product.selling_price  BETWEEN $price1 AND $price2 ";
			} else if(!empty($price1)){
			$where = "product.selling_price  >= $price1 ";
			}else{ $where = "product.selling_price  >=0 ";}
			if($name!=''){
			$where.= "AND product.name  LIKE '$name%' ";
			}
			
			if($location!=''){
			$where.= "AND branch_product.id_branch  LIKE '$location%' ";
			}
			if($code!=''){
			$where.= "AND product.code  LIKE '$code%' ";
			}
			if($category!=''){
			$where.= "AND category.name  LIKE '$category%' ";
			}
			if($company!=''){
			$where.= "AND company.name  LIKE '$company%' ";
			}
  if($location !="")
  {
	$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit','branch_product' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
	$this->db->join('branch_product', 'branch_product.id_product=product.id');

	$this->db->where($where);
	$all_sales=$this->db->get();
	return $all_sales;
}
else{
$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
    $this->db->where($where);
	$all_sales=$this->db->get();
	return $all_sales;
}
}


function getby_name($price1,$price2,$location,$name,$code, $category,$company){
	
			$where = "product.name LIKE '$name%' ";
			if($code!=''){
			$where.= "AND product.code LIKE '$code%' ";
			}
			if($price1!=''){
			 if(!empty ($price2))
			 {
			$where.= "AND product.selling_price  BETWEEN $price1 AND $price2 ";
			} else{
			$where.= "AND product.selling_price  >= $price1 ";
			}
			
			}
			if($location!=''){
			$where.= "AND branch_product.id_branch  LIKE '$location%' ";
			}
			
			if($category!=''){
			$where.= "AND category.name  LIKE '$category%' ";
			}
			if($company!=''){
			$where.= "AND company.name  LIKE '$company%' ";
			}

	if($location !="")
	{
		$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit','branch_product' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
	$this->db->join('branch_product', 'branch_product.id_product=product.id');
$this->db->where($where);
$all_sales=$this->db->get();
return $all_sales;
}else{
$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit');
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
$this->db->where($where);
$all_sales=$this->db->get();
return $all_sales;
}
	
}
function getby_category($price1,$price2,$location,$category, $code, $name,$company){
		
			$where = "category.name LIKE '$category%' ";
			if($code!=''){
			$where.= "AND product.code LIKE '$code%' ";
			}
			if($location!=''){
		$where.= "AND branch_product.id_branch  LIKE '$location%' ";
			}
			if($price1!=''){
			 if(!empty ($price2))
			 {
			$where.= "AND product.selling_price  BETWEEN $price1 AND $price2 ";
			} else{
			$where.= "AND product.selling_price  >= $price1 ";
			}
			
			}
			if($name!=''){
			$where.= "AND product.name  LIKE '$name%' ";
			}
			if($company!=''){
			$where.= "AND company.name  LIKE '$company%' ";
			}
       if($location !="")
	   {
		$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit','branch_product' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
	$this->db->join('branch_product', 'branch_product.id_product=product.id');
	$this->db->join('branch','branch_product.id_branch=branch.id');
			$this->db->where($where);
			$all_sales=$this->db->get();
			return $all_sales;
} else{
$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
			$this->db->where($where);
			$all_sales=$this->db->get();
			return $all_sales;


}
	
}

function getby_company($price1,$price2,$location,$company,$category, $code, $name){
		
			$where = "company.name LIKE '$company%' ";
			if($category!=''){
			$where.= "AND category.name LIKE '$category%' ";
			}
			if($location!=''){
		$where.= "AND branch_product.id_branch  LIKE '$location%' ";
			}
			if($price1!=''){
			 if(!empty ($price2))
			 {
			$where.= "AND product.selling_price  BETWEEN $price1 AND $price2 ";
			} else{
			$where.= "AND product.selling_price  >= $price1 ";
			}
			
			}
			
			
			if($code!=''){
			$where.= "AND product.code LIKE '$code%' ";
			}
			if($name!=''){
			$where.= "AND product.name  LIKE '$name%' ";
			}
    if($location !="")
	{
		$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit','branch_product' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
	$this->db->join('branch_product', 'branch_product.id_product=product.id');

			$this->db->where($where);
			$all_sales=$this->db->get();
			return $all_sales;
}else{
$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name' );
	$this->db->from('product', 'category','company','unit' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
	

			$this->db->where($where);
			$all_sales=$this->db->get();
			return $all_sales;

}

	
}
function getby_location($price1,$price2,$company,$location,$category, $code, $name){
		
			//$where = "product.id_branch LIKE '$location%' ";
			$where= "branch.id  LIKE '$location%' ";
			if($category!=''){
			$where.= "AND category.name LIKE '$category%' ";
			}
			if($company!=''){
			$where.= "AND company.name LIKE '$company%' ";
			}
			if($price1!=''){
			 if(!empty ($price2))
			 {
			$where.= "AND product.selling_price  BETWEEN $price1 AND $price2 ";
			} else{
			$where.= "AND product.selling_price  >= $price1 ";
			}
			
			}
			if($code!=''){
			$where.= "AND product.code LIKE '$code%' ";
			}
			if($name!=''){
			$where.= "AND product.name  LIKE '$name%' ";
			}

		$this->db->select('product.*,category.name AS category_name,company.name AS company_name,unit.name as unit_name,branch.name as branch,branch_product.opening_stock as stock' );
	$this->db->from('product', 'category','company','unit','branch','branch_product');
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
	$this->db->join('branch_product', 'branch_product.id_product=product.id');
$this->db->join('branch','branch_product.id_branch=branch.id');
			$this->db->where($where);
			$all_sales=$this->db->get();
			return $all_sales;


	
}

public function get_all_category()
{
return $this->db->get('category')->result();
}
public function get_all_branch()
{
return $this->db->get('branch')->result();
}
public function get_all_product()
{
return $this->db->get('product')->result();
}
public function get_pending_transfer($id)
{
$data=$this->db->get_where('transfer',array('id' => $id))->row();
return $data;

}
public function get_pending_transfer_product($id)
{
$data=$this->db->get_where('transfer_product',array('transfer_id' => $id))->result();
return $data;

}
public function get_avail_qty($id,$id_branch)
{

$data=$this->db->get_where('branch_product',array('id_product' => $id,'id_branch'=>$id_branch,));
return $data;
}
public function get_product_name($id)
{

$data=$this->db->get_where('product',array('id' => $id,));
return $data;
}
public function update_branch_product($transfer,$id_product,$source,$destination)
{ 

$data=$this->db->get_where('branch_product',array('id_branch' => $source,'id_product'=>$id_product))->row();

if(!empty($data))
{
$new_stock=intval($data->opening_stock)+intval($transfer);
$new=array('opening_stock'=>$new_stock,);
$this->db->update('branch_product',$new,array('id_branch'=>$source,'id_product'=>$id_product,));
$data1=$this->db->get_where('branch_product',array('id_branch' => $destination,'id_product'=>$id_product))->row();
$new_stock_des=intval($data1->opening_stock)-intval($transfer);
$new1=array('opening_stock'=>$new_stock_des,);
$this->db->update('branch_product',$new1,array('id_branch'=>$destination,'id_product'=>$id_product,));
}
else
{
$new_stock=intval($data->opening_stock)+intval($transfer);
$new=array('opening_stock'=>$new_stock,
            'id_branch'   =>$source,
			'id_product'  =>$id_product,);
$this->db->insert('branch_product',$new);
$new_stock_des=intval($data1->opening_stock)-intval($transfer);
$new1=array('opening_stock'=>$new_stock_des,);
$this->db->update('branch_product',$new1,array('id_branch'=>$destination,'id_product'=>$id_product,));
}
}

public function update_transfer_products($qty,$transfer_product_id,$id_product,$transfer_id)
{
$data=$this->db->get_where('transfer_product',array('id' => $transfer_product_id,'id_product'=>$id_product))->row();
$new=array( 'received_quantity'=>$qty,);
$this->db->update('transfer_product',$new,array('id' => $transfer_product_id,'id_product'=>$id_product));

}
public function gets_transfer_detail($id)
{
$this->db->select('*');
$this->db->from('transfer_product');	
$this->db->where('transfer_product.transfer_id',$id);
$this->db->where('transfer_product.quantity != transfer_product.received_quantity');

	$q = 	$query = $this->db->get()->result();
	return $q;
	 

}
public function get_previous_transfer($id)
{
$data=$this->db->get_where('transfer',array('id' => $id))->row();
return $data;

}
public function create_new_transfer($data)
{
$this->db->insert('transfer_product',$data);
}

public function update_approved_transfer($id)
{
$new=array('status'=>'Approved',);
$this->db->update('transfer',$new,array('id'=>$id));
}
public function get_code_name($code)
{
$data=$this->db->get_where('product',array('code' => $code))->row();
return $data;

}



}
